<template>
  <el-select
    v-model="selectedColor"
    class="color-select"
    popper-class="color-popper"
    clearable
  >
    <template #prefix>
      <span
        v-if="selectedColor"
        class="selected-color-block"
        :style="{ backgroundColor: selectedColor }"
      ></span>
    </template>
    <el-option v-for="color in presetColors" :key="color" :label="color" :value="color">
      <div class="color-option">
        <span class="color-block" :style="{ backgroundColor: color }"></span>
        <el-icon v-if="selectedColor === color" class="check">
          <CircleCheck />
        </el-icon>
      </div>
    </el-option>
  </el-select>
</template>

<script setup>
import { ref, watch } from "vue";
import { CircleCheck } from "@element-plus/icons-vue";

const presetColors = [
  "#FF6B6B",
  "#4ECDC4",
  "#45B7D1",
  "#96CEB4",
  "#FFEEAD",
  "#FF9F76",
  "#CC527A",
  "#E8175D",
  "#474747",
  "#363636",
  "#A7226E",
  "#EC2049",
];
const props = defineProps({
  modelValue: {
    type: String,
    default: "",
  },
});
const emit = defineEmits(["select", "update:modelValue"]);
const selectedColor = ref(props.modelValue);
watch(selectedColor, value => {
  emit("update:modelValue", value);
  emit("select", value);
});
</script>

<style scoped>
.color-select {
  --select-width: 240px;
  width: var(--select-width);
}

.color-option {
  display: flex;
  align-items: center;
  padding: 8px 12px;
  position: relative;
}

.color-block {
  width: 100%;
  height: 24px;
  border-radius: 4px;
  margin-right: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.color-label {
  flex: 1;
  font-family: "JetBrains Mono", monospace;
}

.check {
  color: var(--el-color-primary);
  font-size: 16px;
}
.selected-color-block {
  width: 55px;
  height: 18px;
  border-radius: 3px;
  margin: 5px 8px;
}

.el-select__prefix {
  display: flex;
  align-items: center;
}
</style>

<style>
/* 全局覆盖下拉面板样式 */
.color-popper {
  .el-select-dropdown__item {
    padding: 0 !important;
    height: auto;
  }

  .el-select-dropdown__list {
    padding: 8px 0;
  }

  .el-select-dropdown__item.selected {
    background: var(--el-color-primary-light-9);
  }

  .el-scrollbar__wrap {
    padding-bottom: 0 !important;
  }
}
</style>
